package cate.game.res;

import cate.common.table.item.IBagItemTpl;
import cate.game.GameBody;
import cate.game.role.bag.item.BaseBagItem;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass(value = "奖励展示", detail = "用于那些需要同时展示模板数据 实例数据奖励的功能，比如献祭、英雄回退")
public class RewardShow {

    @NoteField(value = "模板奖励")
    public MixRes tplRwd = new MixRes();

    @NoteField(value = "实例奖励")
    public Map<Byte, List<String>> insRwd = new HashMap<>();

    // 添加模板奖励
    public void addTplRwd(MixRes add) {
        if (add == null || add.isEmpty()) {
            return;
        }
        tplRwd.addList(add.copy());
    }
    // 添加模板奖励
    public void addTplItem(MixResItem item) {
        if (item == null || item.num <= 0) {
            return;
        }
        tplRwd.addItem(item.copy());
    }

    public void addBagInstance(BaseBagItem item) {
        if (item == null || item.num <= 0) {
            return;
        }
        tplRwd.items.add(new MixResItem(item));
    }

    // 添加实例奖励
    public void addInsRwd(byte type, String idHex) {
        insRwd.computeIfAbsent(type, k -> new ArrayList<>()).add(idHex);
    }

    public void addBagItem(GameBody game,BaseBagItem e){
        IBagItemTpl row = e.getTpl(game.fight.getPlayBuildContext());
        if (row == null || row.unique() || e.unique()) {
            addInsRwd(e.type, e.uid);
        } else {
            addTplItem(new MixResItem(e.type, e.tid, 1));
        }
    }

    // 合并奖励
    public void merge(RewardShow show) {
        if (show == null || show.isEmpty()) {
            return;
        }
        tplRwd.addList(show.tplRwd);
        for (Map.Entry<Byte, List<String>> entry : show.insRwd.entrySet()) {
            insRwd.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).addAll(entry.getValue());
        }
    }

    public boolean isEmpty() {
        return tplRwd.isEmpty() && insRwd.isEmpty();
    }
}
